﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

/*
 * NOTE TO NATE: For your highscore form, make 6 seperate text boxes for names, and 6 seperate boxes for the names
 * then game control will handle the rest
 * 
 * !!!IMPORTANT!!!! Your class needs to hold an ARRAY of highscores, and then have a variable for a newfound highscore, that can be added into the array
 * make sure to have a method to orangize the array in order of highest score, and not to lose the name associated with it.
 * this will be refered to when assigning these values to the textboxes on the form
 * there should also be another array that will hold the names of each highscore, whose index will corrispond with the highscore array.
 * also, come up with some placeholder values for the highscores constructor, so that the highscore form will not be blank
 * 
 */


namespace HuntTheWumpus
{
	class Highscore
	{
		List<HighScoreItem> highScores = new List<HighScoreItem>();
		string[] textfiles = { "HighScores.txt" };
		GameControl gc = new GameControl();
		

		public Highscore()
		{

			HighScoreItem H1 = new HighScoreItem("Ash", 50, "Cave1");
			HighScoreItem H2 = new HighScoreItem("Misty", 60, "Cave1");
			HighScoreItem H3 = new HighScoreItem("Brock", 30, "Cave1");
			HighScoreItem H4 = new HighScoreItem("Gary", 70, "Cave1");
			HighScoreItem H5 = new HighScoreItem("Professor Oak", 20, "Cave1");
			HighScoreItem H6 = new HighScoreItem("Donnelly", 10, "Cave1");

			highScores.Add(H1);
			highScores.Add(H2);
			highScores.Add(H3);
			highScores.Add(H4);
			highScores.Add(H5);
			highScores.Add(H6);

			SortList();
		}

		

		public void addHighscore()
		{
			int tempScore;
			string name = "" + GameControl.mainPlayer.name;
			string cave = "";
			tempScore = 100 - GameControl.mainPlayer.Turns + GameControl.mainPlayer.Gold + (10 * GameControl.mainPlayer.Arrow);

			HighScoreItem H1 = new HighScoreItem(name, tempScore, cave);
			highScores.Add(H1);

			SortList();

			if (highScores.Count > 10)
			{
				highScores.RemoveAt(10);
			}
		}

		private void SortList()
		{
			for (int x = 0; x < highScores.Count; x++)
			{
				int curScore = highScores[x].Score;

				for (int y = x + 1; y < highScores.Count; y++)
				{
					if (highScores[y].Score > curScore)
					{

						HighScoreItem tempHS = highScores[x];
						highScores[x] = highScores[y];
						highScores[y] = tempHS;
						curScore = highScores[x].Score;
					}
				}



			}
		}

		private void ReadFromFile(int index)
		{

			StreamReader sr = new StreamReader(textFiles[index]);


			string input = sr.ReadLine();


			while (input != null)
			{

				string[] place = input.Split(',');

				string name = place[0];
				int score = int.Parse(place[1]);
				string cave = place[2];



				HighScoreItem highscore = new HighScoreItem(name, score, cave);


				highScores.Add(highscore);


				input = sr.ReadLine();
			}
			
			
			sr.Close();
		}

		private void WriteToFile()
		{

			StreamWriter sw = new StreamWriter("HighScores.txt");

			string output;


			foreach (HighScoreItem highscore in highScores)
		{

				output = highscore.Name + "," +
						 highscore.Score.ToString() + "," +
						 highscore.Cave;


				sw.WriteLine(output);
			}


			sw.Close();
		}
		}
		
        //private void ReadFromFile(int index)
        //{
			
        //    StreamReader sr = new StreamReader(textFiles[index]);

			
        //    string input = sr.ReadLine();

			
        //    while (input != null)
        //    {
				
        //        string[] place = input.Split(',');
				
        //        string name = place[0];
        //        int score = int.Parse(place[1]);
        //        string cave = place[2];


				
        //        HighScoreItem highscore = new HighScoreItem(name, score, cave);

				
        //        highScores.Add(highscore);

				
        //        input = sr.ReadLine();
        //    }

			
        //    sr.Close();
        //}

        //private void WriteToFile()
        //{
			
        //    StreamWriter sw = new StreamWriter("HighScores.txt");

        //    string output;

			
        //    foreach (HighScoreItem highscore in highScores)
        //    {
				
        //        output = highscore.name + "," +
        //                 highscore.score.ToString() + "," +
        //                 highscore.cave;


        //        sw.WriteLine(output);
        //    }

			
        //    sw.Close();
        //}


	}
	
	
}

